package com.wcity.manage.common.utils;

import java.security.MessageDigest;

/**
 * MD5摘要工具类
 *
 * @author Hanley
 */
public class DigestUitl {

	/**
	 * 32位小写加密
	 *
	 * @param src
	 * @return
	 */
	public static String MD5LowerCase(String src) {

		StringBuffer buf = new StringBuffer();
		try {
			MessageDigest digest = MessageDigest.getInstance( ALGOR );
			digest.update( src.getBytes( ENCODE ) );
			byte[] b = digest.digest();
			int i = 0;
			for ( int offset = 0; offset < b.length; offset++ ) {
				i = b[offset];
				if (i < 0) i += 256;
				if (i < 16) buf.append( "0" );
				buf.append( Integer.toHexString( i ) );
			}
		} catch ( Exception e ) {
			e.printStackTrace();
			return "";
		}
		return buf.toString();
	}

	/**
	 * 密码混淆
	 *
	 * @param src
	 * @return
	 */
	public final static String pwd( String src ) {

		String str = MD5LowerCase( src + SALT );
		StringBuilder pwd = new StringBuilder();
		pwd.append( str.substring( 5, 10 ) );
		pwd.append( str.substring( 0, 5 ) );
		pwd.append( str.substring( 20, 32 ) );
		pwd.append( str.substring( 10, 20 ) );
		return pwd.toString();
	}

	private static final String ENCODE = "UTF-8";

	private static final String ALGOR = "MD5";

	private static final String SALT = "*(&JLNdd>?<|~!DDA@#@";
}
